/*----------------------------
- LEDA converter
- Made by Marcus Renno on 06/15/2013
------------------------------
- The purpose of this program is to convert a EDGE list file to a LEDA file.
- The input is called testGraph.txt
- The output is throw into a goldenLeda.gw file
------------------------------*/

#include <list> //to create lists of nodes
#include <iostream>
#include <stdio.h>
#include <boost/foreach.hpp>
#include <fstream> //to use the cpp code of i/o files

using namespace std;
using namespace boost;


struct edgeList {
    char* node;
    char* connected;
    int nodeNumber;
    int connectedNumber;
} typedef edgeList;

int findNode(list<string> node, string search );

int main()
{
    //edgeFile is using FILE, because it is using C (simple reading with fscanf)
    FILE *edgeFile;
    edgeFile = fopen("testGraph.txt", "r");

    //ledaFile is using ofstream, because it is using C++ (list <string>)
    ofstream ledaFile;
    ledaFile.open ("goldenLeda.gw", ios::out);


    //defining the lists here
    typedef list<string> LISTNODES;

    LISTNODES::iterator ite;
    LISTNODES node;

    typedef list<edgeList> LISTEDGE;
    LISTEDGE::iterator iteratorEdge2;
    LISTEDGE nodes;
    //end of the lists definition

    //strings used to buffer the name of the nodes. I thought 30 char's would be enough
    char str[30], str2[30];

    if (edgeFile) {
        while (!feof(edgeFile)) {
            fscanf(edgeFile,"%s ",str);
            fscanf(edgeFile,"%s\n",str2);
            node.insert(node.end(),str);
            node.insert(node.end(),str2);

            //create a edgeList
            //example: node1->node2
            //         node2->node2
            //         node2->node3
            edgeList temp;
            temp.node = new char[101];
            temp.connected = new char[101];

            strcpy(temp.node, str);
            strcpy(temp.connected, str2);
            nodes.insert(nodes.end(),temp);
        }
    }
    //eliminate repetitions
    node.sort();
    node.unique();



    //show the edgeList
    //for (iteratorEdge2 =  nodes.begin(); iteratorEdge2 != nodes.end(); ++iteratorEdge2)
    //    cout << iteratorEdge2->node << " -> " << iteratorEdge2->connected << endl;

    //end of the edgeFile
    fclose(edgeFile);



    //start to create the Leda graph file
    //the header has the information about the graph
    ledaFile << "#header section\n";
    ledaFile << "LEDA.GRAPH\n";
    ledaFile << "string\n"; //node type
    ledaFile << "int\n"; //edge type
    ledaFile << "-2\n"; //direction of the graph -1 is directed, -2 is undirected

    ledaFile << "#nodes section\n";
    //nodes here
    ledaFile << node.size() << endl;
    for (ite =  node.begin(); ite != node.end(); ++ite)
        ledaFile << *ite << endl;
    //edge section
    ledaFile << "\n#edges section\n";
    ledaFile << nodes.size() << endl;
    for (iteratorEdge2 =  nodes.begin(); iteratorEdge2 != nodes.end(); ++iteratorEdge2) {
        //in this section the code is writing the parameters:
        //1) the number of the source node
        //2) the number of the target node
        //3) the number of the reversal edge or 0, if no such edge is set
        //4) the information associated with the edge
        ledaFile << findNode(node,iteratorEdge2->node) << " " << findNode(node,iteratorEdge2->connected); //1) and 2)
        ledaFile << " 0 |{}|" << endl; //3) and 4)
    }
    ledaFile.close();


    return 0;
}

//findNode finds where is the node in the list and return its number
//the code is working well because there are no many nodes in the database
int findNode(list<string> node, string search ) {
    int value = 1;
    string actual;

    list<string>::iterator ite;

    for (ite = node.begin(); ite != node.end(); ++ite, value++)
        {
            actual = *ite;
            if (search.compare(actual) == 0) {
                    return value;
            }
        }

    return -1;
}
